package com.jshoperxms.controller.utils;


import com.jshoperxms.components.upload.qiniu.ImgFileBean;
import com.jshoperxms.controller.utils.json.GsonJson;
import com.jshoperxms.controller.utils.statickey.DefaultValue;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.entity.BasicUserT;
import com.jshoperxms.entity.JshopbasicInfoT;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class BaseTools {
	
	private static final Logger log = LoggerFactory.getLogger(com.jshoperxms.controller.utils.BaseTools.class);
	/**
	 *获取登录用户保存在session中的userid
	 * 
	 * @return
	 */
	public static String getAdminCreateId() {
		ServletRequestAttributes attr = (ServletRequestAttributes)
				RequestContextHolder.currentRequestAttributes();
		HttpServletRequest request = attr.getRequest();
		BasicUserT userT = (BasicUserT) request.getSession().getAttribute(com.jshoperxms.controller.utils.statickey.StaticKey.BACK_USER_SESSION_KEY);
		if (userT!=null) {
			return userT.getId();
		}
		return DefaultValue.DEFAULTADMINID;
	}
	/**
	 *获取登录用户保存在session中的username
	 * 
	 * @return
	 */
	public static String getAdminCreateName() {
		ServletRequestAttributes attr = (ServletRequestAttributes)
				RequestContextHolder.currentRequestAttributes();
		HttpServletRequest request = attr.getRequest();
		BasicUserT userT = (BasicUserT) request.getSession()
				.getAttribute(com.jshoperxms.controller.utils.statickey.StaticKey.BACK_USER_SESSION_KEY);
		if (userT!=null) {
			return userT.getUsername();
		}
		return DefaultValue.DEFAULTADMINNAME;
	}
	


	/**
	 * 获取店铺信息
	 * @return
	 */
	public static JshopbasicInfoT getStoreInfo(){
		ServletRequestAttributes attr = (ServletRequestAttributes)
				RequestContextHolder.currentRequestAttributes();
		HttpServletRequest request = attr.getRequest();
		JshopbasicInfoT jT = (JshopbasicInfoT) request.getSession()
				.getAttribute(com.jshoperxms.controller.utils.statickey.StaticKey.BASIC_SHOP_INFO);
		if (jT!=null) {
			return jT;
		}
		return null;
	}

	

	
	
	/**
	 * 获取网站根目录
	 * 
	 * @return
	 */
	public static String getBasePath() {
		ServletRequestAttributes attr = (ServletRequestAttributes)
				RequestContextHolder.currentRequestAttributes();
		HttpServletRequest request = attr.getRequest();
		String path = request.getContextPath();
		return path;
	}


	/**
	 * 排序
	 * @param param
	 * @param ordervalue
	 * @param ordertype
	 * @return
	 */
	public static Order orderByParam(String param, String ordervalue, String ordertype) {
		Order order=Order.desc(TagWords.UPDATETIME);
		if(StringUtils.isNotBlank(ordertype)){
			if(StringUtils.equals(StaticKey.ASC,ordertype)){
				if(StringUtils.equals(ordervalue,StaticKey.TWO)){
					order = Order.asc(param);
				}else{
					order=Order.asc(TagWords.UPDATETIME);
				}
			}else{
				if(StringUtils.equals(ordervalue,StaticKey.TWO)){
					order = Order.desc(param);
				}else{
					order=Order.desc(TagWords.UPDATETIME);
				}
			}
		}
		return order;
	}

	/**
	 * 转换上传的图片
	 * @param str
	 * @return
	 */
	public static Map<String,String> parseImg(String str){
		Map<String,String>map=new HashMap<>();
		List<ImgFileBean> ifbs= GsonJson.parseJsonToData(str,ImgFileBean.class);
		if(!ifbs.isEmpty()){
			if(StringUtils.isNotBlank(ifbs.get(0).getNormalfilepath())){
				map.put(TagWords.NORMALFILEPATH,ifbs.get(0).getNormalfilepath());
			}else{
				map.put(TagWords.NORMALFILEPATH,StaticKey.EMPTY);
			}
			if(StringUtils.isNotBlank(ifbs.get(0).getCompressfilepath())){
				map.put(TagWords.COMPRESSFILEPATH,ifbs.get(0).getCompressfilepath());
			}else{
				map.put(TagWords.COMPRESSFILEPATH,StaticKey.EMPTY);
			}
		}else{
			map.put(TagWords.NORMALFILEPATH,StaticKey.EMPTY);
			map.put(TagWords.COMPRESSFILEPATH,StaticKey.EMPTY);
		}
		return map;
	}


}
